package OrdersSystem;

import DBSystem.*;

/**
 * 
 * This class contains the attributes, which uniquely specify an alert message,
 * which has to be sent to a particular user. 
 * 
 * @author iSolutions-Team Orders
 *
 */
public class UserAlert {

	/**
	 * Represents the contents of an alert message.
	 */
	private String alertMessage;
	
	/**
	 * Represents the number, which uniquely represents an alert.
	 */	
	private int alertID;
	
	/**
	 * Represents the number, which uniquely represents an user.
	 */	
	private int userID;
	
	/**
	 * Represents the interface, for the database communication.
	 */		
	private static DBSystemComm dbComm = new DBSystemComm();
	
	// Constructor
	/**
	 * A Default Constructor for this class.
	 * 
     */
	public UserAlert() {}
	
	/**
	 * A Constructor for this class, which takes the alertID as an input.
	 * 
	 * @param userID  the number, which uniquely specifies an alert. 
	 * 
     */	
	public UserAlert(int alertID) {
		this.alertID = alertID;
	}
	
	/**
	 * A Constructor for this class, which populates the class with all 
	 * the necessary information. Used mainly for testing purposes.
	 * 
	 * @param userID  The number, which uniquely specifies an alert. 
	 * 
	 * @param alertMessage The message contained in the alert.
	 * 
	 * @param userID The ID of the user about to receive the alert.
	 * 
     */	
	public UserAlert(int alertID, String alertMessage, int userID){
		this.alertID = alertID;
		this.alertMessage = alertMessage;
		this.userID = userID;
	}
	
	
	// Methods to get private fields
	/**
	 * Gets an attribute
	 * @return alertMessage
	*/
	public String getAlertMessage() {
		return alertMessage;
	}

	/**
	 * Gets an attribute.
	 * @return alertID.
	*/
	public int getAlertID() {
		return alertID;
	}
	
	/**
	 * Gets an attribute.
	 * @return userID.
	*/
	public int getUserID() {
		return userID;
	}
	
	// Methods to set fields
	
	/**
	 * Modifies an attribute.
	 * @param alertMessage.
	*/
	public void setAlertMessage(String alertMessage) {
		this.alertMessage = alertMessage;
	}
	
	/**
	 * Modifies an attribute.
	 * @param alertID.
	*/
	public void setAlertID(int alertID) {
		this.alertID = alertID;
	}
	
	/**
	 * Modifies an attribute.
	 * @param userID.
	*/
	public void setUserID(int userID) {
		this.userID = userID;
	}

	/**
	 * This method receives the contact details, based on the userID. 
	 * and sends that mail to the user.
	 * 
	 * @return the alert message, which has to be sent to a particular user. 
	*/
	public String forwardUserAlert() {
		
		String resultMessage;
		// connect to the database to retrieve the rest details of the alert
		UserAlert alertDetails = dbComm.getAlertByID(alertID);
		if( alertDetails !=null) {
			this.alertMessage = alertDetails.getAlertMessage();
			this.userID = alertDetails.getUserID();		
			// connect to db and get the user's contact details
			ContactDetails contact = new ContactDetails(userID);
			if(contact.getEmail()== null) {
				resultMessage = "There is no user registered with an ID: " + userID + ".";
			}else if (contact.getEmail().equals("")) {
				resultMessage = "There is no e-mail address registered for user " + userID + ".";
			}else {
				// Just simulate that the Alert is forwarded to the user by printing its details
				resultMessage = "The alert has the following content: " + alertMessage + "\n" +
							    "It will be sent to the user with id:" + userID + "\n" +
							    "It will be sent to the followind e-mail: " + contact.getEmail();
			}
		}else {
			resultMessage = "There is no alert with and ID: " + alertID + ".";
		}
		return resultMessage;		
	}
	
}
